package org.jetlinks.pro.things.impl.rpc;

import lombok.AllArgsConstructor;
import org.hswebframework.web.bean.FastBeanCopier;
import org.jetlinks.pro.things.ProviderProperties;
import org.jetlinks.pro.things.ThingsConnector;
import org.jetlinks.pro.things.ThingsConnectorProvider;
import org.jetlinks.pro.things.ThingsManagerContext;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.Map;

@AllArgsConstructor
@Component
public class ThingsRpcConnectorProvider implements ThingsConnectorProvider {

    private final ThingsManagerContext context;


    @Override
    public String getId() {
        return "rpc";
    }

    @Override
    public String getName() {
        return "RPC";
    }

    @Override
    public Mono<ThingsConnector> createConnector(Map<String, Object> configuration) {
        ProviderProperties properties = FastBeanCopier.copy(configuration, ProviderProperties.class);
        properties.validate();
        return context
            .createRpcSupport(properties.getProvider(), properties.getConfiguration())
            .map(ThingsRpcConnector::new);
    }


}
